﻿using Cinemachine;
using MyUtil;
using Unity.Netcode;
using UnityEngine;

public class LevelManager : NetworkBehaviour 
{
    public static LevelManager Instance;
    [SerializeField] Transform playerSpawnTF;
    [SerializeField] Collider2D backgroundCollide;
    [SerializeField] float duration = 30;
    int currentDuration=0;
    Timer durationTimer;
    int currentLevel = 0;
    CinemachineVirtualCamera vcam;
    CinemachineConfiner2D cinemachineConfiner2D;

    public int Level => currentLevel;
    public int Duration => (int)duration;

    private void Awake()
    {
        if (Instance == null)
            Instance = this;
        else
            Destroy(this);         
    }       

    public override void OnNetworkSpawn()
    {        
        if (IsServer)
        {            
            MultiPlayManager.Instance.GeneratePlayer(playerSpawnTF);            
        }

        vcam = GameObject.FindWithTag("VCAM").GetComponent<CinemachineVirtualCamera>();
        cinemachineConfiner2D = vcam.GetComponent<CinemachineConfiner2D>();
        cinemachineConfiner2D.m_BoundingShape2D = backgroundCollide;
        durationTimer = TimerManager.Instance.CreateTimer(duration, OnDurationTimeout);
    }

    protected override void OnNetworkPostSpawn()
    {
        if (IsServer)
        {
            StartLevel();
        }
        vcam.Follow = Knight.Instance.transform;
    }

    public override void OnNetworkDespawn()
    {
        TimerManager.Instance.RemoveTimer(durationTimer);
        vcam.Follow = null;
        cinemachineConfiner2D.m_BoundingShape2D=null;
    }

    private void Update()
    {        
        if (durationTimer.PassTime - currentDuration >= 1)
        {
            currentDuration++;
            MyEvenSystem.OnLevelRemainTimeChanged((int)duration - currentDuration);
        }        
    }
    
    public void TrySetVCamFollow(Transform knightTF)
    {        
        if (vcam.Follow == knightTF)
        {
            for (int i = 0; i < Knight.Instance.transform.parent.childCount; i++)
            {
                var knight = Knight.Instance.transform.parent.GetChild(i);
                if(knight.transform==knightTF)
                    continue;
                if (knight.gameObject.activeInHierarchy)
                {
                    vcam.Follow = knight;
                    break;
                }
            }
        }
    }

    public void StartLevel()
    {
        if ((!IsServer)) return;
        StartLevelRpc();
    }
   
    [Rpc(SendTo.ClientsAndHost)]
    void StartLevelRpc()
    {
        if (IsServer)
        {
            EnemyManager.Instance.StartWork();
        }
        
        vcam.Follow= Knight.Instance.transform;

        durationTimer.Start();
        currentDuration = 0;
        currentLevel++;
        
        MyEvenSystem.OnLevelRemainTimeChanged((int)duration);
        MyEvenSystem.OnLevelChanged.Invoke(currentLevel);
        
        PropManager.Instance.DisableChoose();
    }

    void OnDurationTimeout()
    {
        if((!IsServer)) return;

        EnemyManager.Instance.StopWork();
        NotifyEnableChooseRpc();
    }

    [Rpc(SendTo.ClientsAndHost)]
    void NotifyEnableChooseRpc()
    {        
        Knight.Instance.ResetState();
        PropManager.Instance.EnableChoose();
    }
}